// SplashWnd.cpp: implementation of the CSplashWnd class.
//
//////////////////////////////////////////////////////////////////////

// CG: This file was added by the Splash Screen component.
// Splash.cpp : implementation file
//

#include "stdafx.h"  // e. g. stdafx.h

#include "SplashWnd.h"  // e.g. splash.h
#include <SHLWAPI.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
//   Splash Screen class

CSplashWnd::CSplashWnd(const CString& strBmpPath, CWnd* pParent, SplashStyle eStyle)
{
	m_strBmpPath = strBmpPath;
	m_pParent = pParent;
	m_eStyle = eStyle;
	m_bIsFirstShow = TRUE;
	m_nThreadID = 0;
	m_Event = CreateEvent(NULL, FALSE, FALSE, NULL);
}

CSplashWnd::~CSplashWnd()
{
	CloseHandle(m_Event);
	if(IsWindow(this->GetSafeHwnd()))
		this->PostMessage(WM_CLOSE,0,0);
}

BEGIN_MESSAGE_MAP(CSplashWnd, CWnd)
		//{{AFX_MSG_MAP(CSplashWnd)
		ON_WM_CREATE()
		ON_WM_PAINT()
		ON_WM_TIMER()
		ON_WM_LBUTTONDOWN()
		ON_WM_SETCURSOR()
		//}}AFX_MSG_MAP
END_MESSAGE_MAP()

BOOL CSplashWnd::Create(CWnd* pParentWnd /*= NULL*/)
{	
	BITMAP bm;
	m_bitmap.GetBitmap(&bm);
	
	return CreateEx(0,
		AfxRegisterWndClass(0, AfxGetApp()->LoadStandardCursor(IDC_ARROW)),
		NULL, WS_POPUP | WS_VISIBLE, 0, 0, bm.bmWidth, bm.bmHeight, 
		(pParentWnd != NULL)?pParentWnd->GetSafeHwnd():NULL, NULL);
	
}

void CSplashWnd::HideSplashScreen()
{
	WaitForSingleObject(m_Event, 10000);
	if(IsWindow(this->GetSafeHwnd()))
		this->PostMessage(WM_CLOSE,0,0);

	if(AfxGetMainWnd() != NULL)
		AfxGetMainWnd()->UpdateWindow();
}

int CSplashWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	BITMAP bm;
	m_bitmap.GetBitmap(&bm);
	SetWindowPos(&wndTopMost, 0, 0, bm.bmWidth, bm.bmHeight, SWP_SHOWWINDOW);

	CenterWindow();
		
	return 0;
}

void CSplashWnd::OnPaint()
{
	CPaintDC dc(this);
	
	CDC dcImage;
	if (!dcImage.CreateCompatibleDC(&dc))
		return;
	
	BITMAP bm;
	m_bitmap.GetBitmap(&bm);
	
	// Paint the image.
	CBitmap* pOldBitmap = dcImage.SelectObject(&m_bitmap);
	dc.BitBlt(0, 0, bm.bmWidth, bm.bmHeight, &dcImage, 0, 0, SRCCOPY);
	dcImage.SelectObject(pOldBitmap);
}

void CSplashWnd::OnTimer(UINT nIDEvent)
{
	
}


void CSplashWnd::OnLButtonDown(UINT nFlags, CPoint point)
{
	if(m_eStyle&eLBUTTONHIDEN)
	{
		HideSplashScreen();
	}
}

UESoft::ErrorStatus CSplashWnd::ShowSplash()
{
	CreateThread(NULL, 0, &SplashWndProc, this, 0, &m_nThreadID);
	
	return UESoft::eOk;
}

BOOL CSplashWnd::ShowSplash_()
{
	if(m_bIsFirstShow)
	{
		if(!PathFileExists(m_strBmpPath))
		{
			ASSERT(FALSE);
			return FALSE;
		}
		
		m_bitmap.Attach(::LoadImage(NULL, m_strBmpPath,
			IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE));

		m_bIsFirstShow = FALSE;
	}
	if(GetSafeHwnd() == NULL)
	{
		if(!Create(m_pParent))
			return FALSE;
	}
	else
	{
		return FALSE;
	}

	UpdateWindow();
	ShowWindow(SW_SHOW);
	return TRUE;
}

UESoft::ErrorStatus CSplashWnd::HideSplash()
{
	HideSplashScreen();
	return UESoft::eOk;
}

BOOL CSplashWnd::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
{
	if(m_eStyle&eWAITCURSOR)
	{
		SetCursor(LoadCursor(NULL, IDC_WAIT));
		return TRUE;
	}

	return CWnd::OnSetCursor(pWnd, nHitTest, message);
}

DWORD WINAPI CSplashWnd::SplashWndProc( LPVOID lpWnd )
{
	if(lpWnd == NULL)
		return 0;

	CSplashWnd* pWnd = static_cast<CSplashWnd*>(lpWnd);
	
	if(!pWnd->ShowSplash_())
		return 0;

	MSG msg;

	SetEvent(pWnd->m_Event);
	while (GetMessage (&msg, NULL, 0, 0))
     {
		TranslateMessage (&msg) ;
  		DispatchMessage (&msg) ;
		if(msg.message == WM_CLOSE)
		{
			break;
		}
     }
	return msg.wParam ;


}